package com.ruoyi.web.controller.loss;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.loss.domain.bo.WellLostRecordBo;
import com.ruoyi.loss.domain.vo.WellLostRecordVo;
import com.ruoyi.loss.service.IWellLostRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 井漏记录
 *
 * @author yangyong
 * @date 2023-12-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/loss/wellLostRecord")
public class WellLostRecordController extends BaseController {

    private final IWellLostRecordService iWellLostRecordService;

    /**
     * 查询井漏记录列表
     */
    @SaCheckPermission("helper:wellLostRecord:list")
    @GetMapping("/list")
    public TableDataInfo<WellLostRecordVo> list(WellLostRecordBo bo, PageQuery pageQuery) {
        return iWellLostRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出井漏记录列表
     */
    @SaCheckPermission("helper:wellLostRecord:export")
    @Log(title = "井漏记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WellLostRecordBo bo, HttpServletResponse response) {
        List<WellLostRecordVo> list = iWellLostRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "井漏记录", WellLostRecordVo.class, response);
    }

    /**
     * 获取井漏记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("helper:wellLostRecord:query")
    @GetMapping("/{id}")
    public R<WellLostRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iWellLostRecordService.queryById(id));
    }

    /**
     * 新增井漏记录
     */
    @SaCheckPermission("helper:wellLostRecord:add")
    @Log(title = "井漏记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WellLostRecordBo bo) {
        return toAjax(iWellLostRecordService.insertByBo(bo));
    }

    /**
     * 修改井漏记录
     */
    @SaCheckPermission("helper:wellLostRecord:edit")
    @Log(title = "井漏记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WellLostRecordBo bo) {
        return toAjax(iWellLostRecordService.updateByBo(bo));
    }

    /**
     * 删除井漏记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("helper:wellLostRecord:remove")
    @Log(title = "井漏记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWellLostRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
